import { gsdlist, saveWXAddrees } from "../../../server/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    columns: [],
    fieldNames: {
      text: 'pointsPlaceName',
      value: 'id',
      children: 'children',
    },
    op:[],
    cascaderValue:''
  },
  onChangecascader(e){

     const {value } = e.detail
     console.log(e,'val');
     this.setData({cascaderValue:value})
  },
  //取消
  onCancel() {
    wx.switchTab({
      url: "/pages/pointsmall/pointsmall",
    });
  },
  //确认
  async onConfirm(event) {
      if(this.data.cascaderValue == '')
      {
        wx.showToast({
          title: "请选择归属地",
        });
      }else
      {
        const phone = wx.getStorageSync("phone");
        const parmas = {
          addressCode:this.data.cascaderValue,
          phoneCode: phone,
        };
        const data = await saveWXAddrees(parmas);
        if (data.code == 200) {
          wx.setStorageSync('address', '1')
          wx.switchTab({
            url: "/pages/pointsmall/pointsmall",
          });
        } else {
          wx.showToast({
            title: data.message,
            icon: "error",
            duration: 1000,
          });
        }
      }

      return
    const res = event.detail.value;
    if (res[0].id == 10) {
      wx.showToast({
        title: "请选择归属地",
      });
    } else {
      const phone = wx.getStorageSync("phone");
      const parmas = {
        // addressCode: (res[2] == undefined && res[1] == undefined) ? res[0].id : res[2] == undefined ? res[1].id : res[2].id,
        addressCode:this.data.cascaderValue,
        phoneCode: phone,
      };
      const data = await saveWXAddrees(parmas);
      if (data.code == 200) {
        wx.setStorageSync('address', '1')
        wx.switchTab({
          url: "/pages/pointsmall/pointsmall",
        });
      } else {
        wx.showToast({
          title: data.message,
          icon: "error",
          duration: 1000,
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.getdata();
    wx.hideHomeButton();
  },
  //获取列表数据
  async getdata() {
    const res = await gsdlist();
    if (res.code == 200) {
      const data = res.result;
      this.setData({op:data})
      // data.unshift({
      //   id: "10",
      //   pointsPlaceName: "请选择",
      //   children: [
      //     {
      //       id: "11",
      //       pointsPlaceName: "请选择",
      //       children: [
      //         {
      //           id: "12",
      //           pointsPlaceName: "请选择",
      //         },
      //       ],
      //     },
      //   ],
      // });
      // const list = [
      //   {
      //     values: data,
      //   },
      //   {
      //     values: data[0].children,
      //     defaultIndex: 0,
      //   },
      //   {
      //     values: data[0].children[0].children,
      //     defaultIndex: 0,
      //   },
      // ];
      // this.setData({ columns: list });
    }
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    if (index == 0) {
      picker.setColumnValues(1, value[0].children == null ? [] : value[0].children);
      picker.setColumnValues(2,value[0].children == null ? [] : value[0].children[0].children == null ? [] : value[0].children[0].children);
    } else if (index === 1) {
      picker.setColumnValues(2, value[1].children == null ? [] : value[1].children);
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
